<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\logic\Callback;
use app\logic\CommonLogic;
use app\logic\GoodsLogic;
use app\logic\OrderLogic;
use app\logic\PlayGoodsLogic;
use app\logic\ShopLogic;
use app\models\OrderGoods;
use Yii;
use yii\base\ErrorException;


class ChannelController extends BaseController
{

	public function actionCallback()
	{
        $request = Yii::$app->request->post();

        //$method = $this->getQueryparams('method','');
        file_put_contents(date("Ymd")."_yx.txt", json_encode($request)." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
        //$request = json_decode('{"sign":"E5FAE20409B500A0B16A62D554A1B2EC","orderPackage":"{\"expCreateTime\":1532943989270,\"expressDetailInfos\":[{\"expressCompany\":\"EMS\",\"expressNo\":\"704053\",\"skus\":[{\"activityTotalAmount\":0.00,\"couponTotalAmount\":0.00,\"originPrice\":116.20,\"productName\":\"\u6e20\u9053\u4e66\u67b6\u6d4b\u8bd52121\",\"saleCount\":3,\"skuId\":\"1087002\",\"subtotalAmount\":348.60}],\"storeHouseName\":\"EMS\u5357\u4eac\u4ed3\"}],\"expressInfos\":[{\"expressCompany\":\"EMS\",\"expressNos\":[\"704053\"]}],\"orderId\":\"2018073010148529\",\"packageId\":23791006}","appKey":"e88e364354ce4afc80e6918f418ed742","method":"yanxuan.notification.order.delivered","timestamp":"2018-07-30 17:46:29"}',true);
        if(isset($request['method'])){
            try{
                $method = $request['method'];
                switch($method){
                    case "yanxuan.notification.order.delivered":
                        //$orderPackage = $this->getQueryparams('orderPackage','');
                        $orderPackage = $request['orderPackage'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"delivered",$orderPackage);
                        break;
                    case "yanxuan.notification.order.refund.address":
                        //$refundAddress = $this->getQueryparams('refundAddress','');
                        $refundAddress = $request['refundAddress'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"refundAddress",$refundAddress);
                        break;
                    case "yanxuan.notification.order.refund.reject":
                        //$rejectInfo = $this->getQueryparams('rejectInfo','');
                        $rejectInfo = $request['rejectInfo'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"refundReject",$rejectInfo);
                        break;
                    case "yanxuan.notification.order.refund.express.confirm":
                        //$expressConfirm = $this->getQueryparams('expressConfirm','');
                        $expressConfirm = $request['expressConfirm'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"expressConfirm",$expressConfirm);
                        break;
                    case "yanxuan.notification.order.refund.result":
                        //$refundResult = $this->getQueryparams('refundResult','');
                        $refundResult = $request['refundResult'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"refundResult",$refundResult);
                        break;
                    case "yanxuan.notification.order.refund.system.cancel":
                        //$systemCancel = $this->getQueryparams('systemCancel','');
                        $systemCancel = $request['systemCancel'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"systemCancel",$systemCancel);
                        break;
                    case "yanxuan.notification.order.exceptional":
                        //$exceptionInfo = $this->getQueryparams('exceptionInfo','');
                        $exceptionInfo = $request['exceptionInfo'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"exceptional",$exceptionInfo);
                        break;
                    case "yanxuan.callback.order.cancel":
                        //$orderCancelResult = $this->getQueryparams('orderCancelResult','');
                        $orderCancelResult = $request['orderCancelResult'];
                        $orderLogic = new OrderLogic;
                        $callback = new Callback($orderLogic,"cancelCallback",$orderCancelResult);
                        break;
                    case "yanxuan.callback.item.change":
                        $itemChangeResult = $request['itemChange'];
                        $goodsLogic = new GoodsLogic;
                        $callback = new Callback($goodsLogic,"itemChangeCallback",$itemChangeResult);
                        break;
                    case "yanxuan.notification.inventory.count.check":
                        $skuCheck = $request['skuCheck'];
                        $goodsLogic = new GoodsLogic;
                        $callback = new Callback($goodsLogic,"inventoryCheck",$skuCheck);
                        break;
                    case "yanxuan.notification.sku.alarm.close":
                        $closeAlarmSkus = $request['closeAlarmSkus'];
                        $goodsLogic = new GoodsLogic;
                        $callback = new Callback($goodsLogic,"skuAlarm",$closeAlarmSkus);
                        break;
                    case "yanxuan.notification.sku.reopen":
                        $reopenSkus = $request['reopenSkus'];
                        $goodsLogic = new GoodsLogic;
                        $callback = new Callback($goodsLogic,"skuReopen",$reopenSkus);
                        break;
                }
                $callback->run();
            }catch(ErrorException $e){
                echo json_encode(['code'=>200,'msg'=>'']);exit;
            }
        }
        echo json_encode(['code'=>200,'msg'=>'']);exit;
	}

	public function actionTestCallback(){
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackAuditCancelOrder','/callbackAuditCancelOrder',['orderId'=>'2018073053559851','cancel'=>true]);
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackCancelOrder','/callbackCancelOrder',['orderId'=>'2018073053975556']);
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackDeliveryOrder','/callbackDeliveryOrder',['orderId'=>'2018081698975551']);
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackRefundReject','/callbackRefundReject',['applyId'=>'2463141']);
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackRefundAddress','/callbackRefundAddress',['applyId'=>'2537144','type'=>1]);
        $result = HttpCurl::callback_post('/mock/api/v1/callbackRefundExpressConfirm','/callbackRefundExpressConfirm',['applyId'=>'2537144']);
        //$result = HttpCurl::callback_post('/mock/api/v1/callbackRefundResultWithExpress','/callbackRefundResultWithExpress',['applyId'=>'2468142','allApproved'=>true]);

        var_dump($result);exit;
	}

	public function actionCallbackMethodList(){
        print_r(HttpCurl::post("yanxuan.callback.method.list"));
    }

}